package com.google.wavesurferrobot.textmining;

/*
*************************************************************************************************
* File:         Category.java                                                                   *
* Usage:        Category is used to hold the documents and the learned model of a Category      *
*************************************************************************************************
* IDE/Compiler: For the development needs we used NetBeans 5.0 and JRE 1.5.0_06 on WinXP Home   *
*************************************************************************************************
* License:      (LGPL) GNU Lesser General Public License                                        *
*               http://www.opensource.org/licenses/lgpl-license.php                             *
*************************************************************************************************
*         Originally written by George Tsatsaros (gbt@aueb.gr)                                  *      
*               Author         :  Panayiotis Papadopoulos (3010010)                             *
*               Website        :  http://dias.aueb.gr/~p3010010/                                *
*               E-mail         :  papado@freemail.gr                                            *
*                                 p3010010@dias.aueb.gr                                         *
*               MSN messenger  :  pap5@hotmail.com                                              *
*               Skype          :  papOnSlayer                                                   *
*                                                                                               *
* Contact:  Feel free to contact with me for any question/suggestion using the email(s) above   *
*************************************************************************************************
*/

import java.util.Enumeration;
import java.util.Hashtable;


public class Category
{
    private Hashtable categoryDocuments;
    private Hashtable learnedModel;
    private String catName;
    private int catTokensNum;

    //Probability of category -> Pr(Ci)
    private double probC;


    public Category(Hashtable theDocuments, String categoryName)
    {
        categoryDocuments = theDocuments;
        catName = categoryName;
        learnedModel = new Hashtable();
        computeCategoryTokens();
    }

    private void computeCategoryTokens()
    {
        catTokensNum = 0;

        for (Enumeration e = categoryDocuments.elements(); e.hasMoreElements(); )
        {
            Document docObj = (Document) e.nextElement();
            catTokensNum += docObj.getSN();
        }
    }

    public String getCategoryName()
    {
        return catName;
    }

    public Hashtable getCategoryDocuments()
    {
        return categoryDocuments;
    }

    public int getDocumentsNum()
    {
        return categoryDocuments.size();
    }

    public int getCategoryTokensNum()
    {
        return catTokensNum;
    }

//probC

    public void computeCategoryProbability(int numOfTokens)
    {
        //Pr(Ci) = (number of tokens in Ci) / (total number of tokens)
        probC = (double) catTokensNum / (double) numOfTokens;
    }

    /**
     * 
     * @return 
     */
    public double getCategoryProbability()
    {
        return probC;
    }


//TokenOccurences

    public int getTokenOccurences(String token)
    {
        int sum = 0;

        for (Enumeration e = categoryDocuments.elements(); e.hasMoreElements(); )
        {
            Document docObj = (Document) e.nextElement();

            if (docObj.tokenExists(token))
                sum += ((Integer) docObj.getTFFor(token)).intValue();
        }

        return sum;
    }

    public int getNotTokenOccurences(String token)
    {
        return catTokensNum - getTokenOccurences(token);
    }


//learnedModel

    private void addTokenToLearnedModel(String token, Double TFIDF)
    {
        if (learnedModel.containsKey(token))
        {
            Double aDouble = (Double) learnedModel.get(token);
            learnedModel.put(token, new Double(aDouble.doubleValue() + TFIDF.doubleValue()));
        }
        else
            learnedModel.put(new String(token), new Double(TFIDF.doubleValue()));
    }


    public void computeLearnedModel()
    {
        for (Enumeration e = categoryDocuments.elements(); e.hasMoreElements(); )
        {
            Document docObj = (Document) e.nextElement();

            for (Enumeration en = docObj.getOrderedTokens().elements(); en.hasMoreElements(); )
            {
                String token = (String) en.nextElement();
                Double TFIDF = (Double) docObj.getCoordinateFor(token);
                addTokenToLearnedModel(token, TFIDF);
            }
        }
    }

    public Hashtable getLearnedModel()
    {
        return learnedModel;
    }

    public Double getLearnedModelFor(String aToken)
    {
        return (Double) learnedModel.get(aToken);
    }

    public boolean tokenExistsLM(String aToken)
    {
        return learnedModel.containsKey(aToken);
    }

}//Class Category.
